package com.rpj.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rpj.entity.Result;
import com.rpj.entity.RolesDto;
import com.rpj.entity.RolesEntity;
import com.rpj.service.RolesService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api("角色的增删改查")
public class RolesController {
@Autowired
public RolesService rolesservice;
@RequestMapping(value="/api/GetRolesLayUI",method = RequestMethod.POST)
@ApiOperation("分页查询")
public Object findRoles(RolesDto Dto) {
	HashMap<String, Object> map = new HashMap<String, Object>();
	List<RolesEntity> list = rolesservice.findRoles(Dto);
	int i = rolesservice.countRoles(Dto);
	map.put("code", 0);
	map.put("msg", "");
	map.put("count",i);
	map.put("data", list);
	return map;
}
@RequestMapping(value="/api/CreateRole",method = RequestMethod.POST)
@ApiOperation("添加")
public Object saveRoles(@ApiParam("名字")@RequestParam("name")String name,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	RolesEntity rolesEntity = new RolesEntity();
	rolesEntity.setName(name);
	int i = rolesservice.saveRoles(rolesEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","添加成功",true);
		 }else{
				return new Result("","添加失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
@RequestMapping(value="/api/UpdateRole",method = RequestMethod.POST)
@ApiOperation("修改角色")
public Object updateRoles(@ApiParam("姓名")@RequestParam("name")String name,@ApiParam("id")@RequestParam("roleId")String roleId,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	RolesEntity rolesEntity = new RolesEntity();
	rolesEntity.setId(roleId);
	rolesEntity.setName(name);
	int i = rolesservice.updateRoles(rolesEntity);
	System.out.println(i);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","修改成功",true);
		 }else{
				return new Result("","修改失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
@RequestMapping(value="/api/DeleteRole",method = RequestMethod.POST)
@ApiOperation("删除角色")
public Object delRoles(@ApiParam("id")@RequestParam("roleId")String roleId,HttpSession session,@ApiParam("密钥")@RequestParam("token")String token) {
	RolesEntity rolesEntity = new RolesEntity();
	rolesEntity.setId(roleId);
	int i = rolesservice.delRoles(rolesEntity);
	String gettoken=(String) session.getAttribute("token");
	if(gettoken.equals(token)) {
		 if(i>0) {
				return new Result("","添加成功",true);
		 }else{
				return new Result("","添加失败",false);
			}
	 }else {
		 return new Result("","密钥错误",false);
	 }
}
}
